﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Admin.NET.Application.Entity;
using Admin.NET.Application.Service.TwoCategories.Dto;
using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;

/// <summary>
/// 二级工单服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class TwoCategoriesService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<TwoCategories> _twoCategoriesRep;
    private readonly SqlSugarRepository<OneCategories> _oneCategoriesRep;
    public TwoCategoriesService(SqlSugarRepository<TwoCategories> twoCategoriesRep, SqlSugarRepository<OneCategories> oneCategoriesRep)
    {
        _twoCategoriesRep = twoCategoriesRep;
        _oneCategoriesRep = oneCategoriesRep;
    }

    /// <summary>
    /// 分页查询二级工单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询二级工单")]
    public async Task<SqlSugarPagedList<TwoCategoriesOutput>> Page(PageTwoCategoriesInput input)
    {
        var query = _twoCategoriesRep.AsQueryable()
            .Where(u => !u.IsDelete)
            .WhereIF(input.OId > 0, u => u.OId == input.OId)
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<OneCategories>((u, oid) => u.OId == oid.Id)
            .Select((u, oid) => new TwoCategoriesOutput
            {
                Id = u.Id,
                OId = u.OId,
                OIdOneCategoriesName = oid.OneCategoriesName,
                TwoCategoriesName = u.TwoCategoriesName,
                Region = u.Region,
                Content = u.Content,
                CreateTime = u.CreateTime,
                UpdateTime = u.UpdateTime,
                CreateUserId = u.CreateUserId,
                CreateUserName = u.CreateUserName,
                UpdateUserId = u.UpdateUserId,
                UpdateUserName = u.UpdateUserName,
                CreateOrgId = u.CreateOrgId,
                CreateOrgName = u.CreateOrgName,
                IsDelete = u.IsDelete,
                Repairs = u.Repairs,
            });
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加二级工单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加二级工单")]
    public async Task<long> Add(AddTwoCategoriesInput input)
    {
        var entity = input.Adapt<TwoCategories>();
        await _twoCategoriesRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除二级工单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除二级工单")]
    public async Task Delete(DeleteTwoCategoriesInput input)
    {
        var entity = await _twoCategoriesRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _twoCategoriesRep.FakeDeleteAsync(entity);   //假删除
        //await _twoCategoriesRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新二级工单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新二级工单")]
    public async Task Update(UpdateTwoCategoriesInput input)
    {
        var entity = input.Adapt<TwoCategories>();
        await _twoCategoriesRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取二级工单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取二级工单")]
    public async Task<TwoCategories> Detail([FromQuery] QueryByIdTwoCategoriesInput input)
    {
        return await _twoCategoriesRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取二级工单列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取二级工单列表")]
    public async Task<List<TwoCategoriesOutput>> List([FromQuery] PageTwoCategoriesInput input)
    {
        return await _twoCategoriesRep.AsQueryable().Select<TwoCategoriesOutput>().ToListAsync();
    }
    /// <summary>
    /// 增加一级工单
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "AddOne"), HttpPost]
    [DisplayName("增加一级工单")]
    public async Task<long> AddOne(OneCategoriesDTO input)
    {
        var entity = input.Adapt<OneCategories>();
        await _oneCategoriesRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 获取大类外键列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "OneCategoriesOIdDropdown"), HttpGet]
    [DisplayName("获取大类外键列表")]
    public async Task<dynamic> OneCategoriesOIdDropdown()
    {
        return await _twoCategoriesRep.Context.Queryable<OneCategories>()
                .Select(u => new
                {
                    Label = u.OneCategoriesName,
                    Value = u.Id
                }
                ).ToListAsync();
    }




}
